<?php
/**
 * Class for sending an email using SMTP
 * @category Bitzero
 * @package Bitzero_Mail
 * @copyright Copyright (c) 2011 PT. Bitzero Informatika (http://www.bitzeroinfo.com)
 */

/**
 * @see Zend_Mail_Transport_Smtp
 */
require_once 'Zend/Mail/Transport/Smtp.php';

/**
 * @see Zend_Mail
 */
require_once 'Zend/Mail.php';

class Bitzero_Mail_Smtp{
    
    /**
     * @var Zend_Mail_Transport_Smtp
     */
    protected $_smtp = null;

    /**
     * Construct SMTP mail object with server is $server and configuration is $config
     * @param string $server
     * @param array $config
     */
    public function  __construct($server, $config = null) {
        $this->_smtp = new Zend_Mail_Transport_Smtp ($server,$config);
    }

    /**
     * Set SMTP object
     * @param Zend_Mail $smtp
     */
    public function setSmtp(Zend_Mail_Transport_Smtp $smtp){
        $this->_smtp = $smtp;
    }

    /**
     * Get SMTP object
     * @return Zend_Mail_Transport_Smtp
     */
    public function getSmtp(){
        return $this->_smtp;
    }

    /**
     * Set default 'from' field of email
     * @param string $email
     * @param string $name
     */
    public function setDefaultFrom($email,$name = null){
        Zend_Mail::setDefaultFrom($email, $name);
    }

    /**
     * Set default 'reply to' field of email
     * @param string $email
     * @param string $name
     */
    public function setDefaultReplyTo($email,$name = null){
        Zend_Mail::setDefaultReplyTo($email, $name);
    }

    /**
     * Send email using SMTP server that is defined when creating
     * this object
     * @param array $to
     * @param array $from
     * @param string $subject
     * @param string $body
     */
    public function send($to,$from,$subject,$body){
        //Send mail for every receivers
        foreach($to as $key => $val){
            $mail = new Zend_Mail();
            $mail->addTo($key,$val);
            if($from!=null) $mail->setFrom (key($from),$from[key($from)]);
            $mail->setSubject($subject);
            $mail->setBodyText($body);
            $mail->send($this->_smtp);
        }

        Zend_Mail::setDefaultFrom("");
        Zend_Mail::setDefaultReplyTo("");
    }
}

?>
